package com.haikesi.api.organization.service.impl;

import com.haikesi.api.base.Constants;
import com.haikesi.api.base.Error;
import com.haikesi.api.base.ErrorTools;
import com.haikesi.api.base.Type;
import com.haikesi.api.organization.dao.OrganizationDao;
import com.haikesi.api.organization.domain.po.OrganizationPO;
import com.haikesi.api.organization.domain.vo.*;
import com.haikesi.api.organization.service.IOrganizationService;
import com.haikesi.exception.BusinessException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName OrganizationServiceImpl
 * @Author HandsomeBugs
 * @Date 2019/4/30 9:51
 * @Version 1.0
 **/
@Service
@Transactional
public class OrganizationServiceImpl implements IOrganizationService {
    @Autowired
    private OrganizationDao dao;


    @Override
    public String create(OrganizationSaveVO seveVO) {
        OrganizationPO po = new OrganizationPO();
        BeanUtils.copyProperties(seveVO, po);
        //需要补充 父级机构ID集合(用逗号隔开所有父级ID) 和 父级机构名字集合
        if (StringUtils.isNotBlank(po.getParentId()) && !po.getParentId().equals("0")) {
            OrganizationPO organizationPO = dao.selectByPrimaryKey(po.getParentId());
            if (organizationPO == null) {
                throw new BusinessException(Type.NOT_FOUND_ERROR, ErrorTools.ErrorAsArrayList(new Error("parentId", "父类不存在")));
            }
            po.setParentIds(organizationPO.getId());
            po.setParentName(organizationPO.getOrganizationName());
            if (!organizationPO.getParentId().equals("0")) {
                po.setParentIds(organizationPO.getParentIds() + "," + organizationPO.getId());
                po.setParentName(organizationPO.getParentName() + "," + organizationPO.getOrganizationName());
            }
        }
        po.preInsert();
        int i = dao.insertSelective(po);
        if (i == Constants.SQL_RETUN_SUCCESS) {
            throw new BusinessException(Type.EXCEPTION_FAIL);
        }
        return po.getId();
    }

    @Override
    public void update(OrganizationUpdateVO updateVO) {

        OrganizationPO po = new OrganizationPO();
        BeanUtils.copyProperties(updateVO, po);

        //需要补充 父级机构ID集合(用逗号隔开所有父级ID) 和 父级机构名字集合
        if (StringUtils.isNotBlank(po.getParentId())) {
            OrganizationPO organizationPO = dao.selectByPrimaryKey(po.getParentId());
            if (organizationPO == null) {
                throw new BusinessException(Type.NOT_FOUND_ERROR, ErrorTools.ErrorAsArrayList(new Error("parentId", "父类不存在")));
            }
            po.setParentIds(organizationPO.getId());
            po.setParentName(organizationPO.getOrganizationName());
            if (!organizationPO.getParentId().equals("0")) {
                po.setParentIds(organizationPO.getParentIds() + "," + organizationPO.getId());
                po.setParentName(organizationPO.getParentName() + "," + organizationPO.getOrganizationName());
            }
        }
        po.preUpdate();
        int i = dao.updateByPrimaryKeySelective(po);
        if (i == Constants.SQL_RETUN_SUCCESS) {
            throw new BusinessException(Type.EXCEPTION_FAIL);
        }
    }

    @Override
    public void delete(String id) {
        if (StringUtils.isBlank(id)) {
            throw new BusinessException(Type.FORMAT_EXCEPTION);
        }
        OrganizationPO organizationPO = dao.selectByPrimaryKey(id);
        if (organizationPO == null) {
            throw new BusinessException(Type.NOT_FOUND_ERROR, ErrorTools.ErrorAsArrayList(new Error("id", "机构不存在")));
        }
        OrganizationPO po = new OrganizationPO();
        po.setId(id);
        po.preDelete();
        int i = dao.updateByPrimaryKeySelective(po);
        if (i == Constants.SQL_RETUN_SUCCESS) {
            throw new BusinessException(Type.EXCEPTION_FAIL);
        }
        //删除下级的机构
        dao.deleteByParentId(id);
    }

    @Override
    public List<OrganizationVO> getList(OrganizationSearchVO searchVO) {
        OrganizationPO po = new OrganizationPO();
        if (po.getUser() == null) {
            searchVO.setOrganizationId("");
        } else {
            searchVO.setOrganizationId(po.getUser().getOrganizationId());
            if (po.getUser().getIsAdmin() == 1) {
                searchVO.setOrganizationId("");
            }
        }
        return dao.getList(searchVO);
    }

    @Override
    public List<OrganizationTreeVO> getTreeList(OrganizationTreeSearchVO searchVO) {
        OrganizationPO po = new OrganizationPO();
        List<OrganizationTreeVO> retunList = new ArrayList<>();
        if (po.getUser() == null) {
            searchVO.setOrganizationId("");
        } else {
            searchVO.setOrganizationId(po.getUser().getOrganizationId());
            if (po.getUser().getIsAdmin() == 1) {
                searchVO.setOrganizationId("");
            }
        }
        List<OrganizationTreeVO> list = dao.getTreeList(searchVO);

        HashMap<String, OrganizationTreeVO> map = new HashMap<>();
        for (OrganizationTreeVO vo : list) {
            map.put(vo.getId(), vo);
        }
        for (OrganizationTreeVO vo : list) {
            if (vo.getParentId().equals("0")) {
                retunList.add(vo);
            } else {
                OrganizationTreeVO parentVO = map.get(vo.getParentId());
                if (parentVO == null) {
                    retunList.add(vo);
                    continue;
                }
                if (parentVO.getChildrens() == null) {
                    parentVO.setChildrens(new ArrayList<>());
                }
                parentVO.getChildrens().add(vo);
            }
        }
        return retunList;
    }
}
